{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 一维数组\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "本代码首先导入了用于数值计算的 NumPy 库，目的是利用其高效的数组操作功能。数学上，我们可以把数据看作一个向量，这里定义的 Python 列表  \n",
    "$$a\\_list = [1,\\,2,\\,3,\\,4,\\,5,\\,6,\\,7,\\,8,\\,9,\\,10]$$  \n",
    "包含 $10$ 个元素。接下来，利用 NumPy 的 array 函数将这个列表转换为一个一维数组，我们将其视为一个行向量，记作  \n",
    "$$a = [1,\\; 2,\\; 3,\\; 4,\\; 5,\\; 6,\\; 7,\\; 8,\\; 9,\\; 10].$$  \n",
    "\n",
    "数组的维数由属性 $a.ndim$ 得到，此处 $dim = 1$，表明这是一个一维向量。数组的形状由 $a.shape$ 给出，返回值为 $(10,)$，说明行向量在其唯一的维度上共有 $10$ 个元素。\n",
    "\n",
    "在索引部分，代码展示了如何访问行向量中的特定元素。注意在 NumPy 中采用零索引法，即第一个元素的索引为 $0$。例如：  \n",
    "- $a[0]$ 返回向量的第一个元素（数学上对应 $a_1$）；  \n",
    "- $a[1]$ 返回第二个元素（对应数学中的 $a_2$）；  \n",
    "- $a[-1]$ 返回最后一个元素（对应数学中的 $a_{10}$）；  \n",
    "- $a[-2]$ 返回倒数第二个元素（对应 $a_9$）。\n",
    "\n",
    "切片操作则用于提取向量的子部分。切片表达式采用半开区间的形式，如 $a[0:2]$ 表示从索引 $0$ 开始，直到索引 $2$（不含 $2$）的所有元素，即得到  \n",
    "$$a_{\\text{slice}} = [1,\\; 2].$$  \n",
    "同样，$a[:2]$ 是简写形式，效果相同。表达式 $a[1:]$ 则提取从索引 $1$（即数学中的 $a_2$）开始到最后的所有元素，得到  \n",
    "$$a_{\\text{slice}} = [2,\\; 3,\\; 4,\\; 5,\\; 6,\\; 7,\\; 8,\\; 9,\\; 10].$$  \n",
    "此外，$a[-3:]$ 和 $a[7:]$ 均提取了最后 $3$ 个元素，因为索引 $7$ 对应的是第 $8$ 个元素，于是得到  \n",
    "$$a_{\\text{slice}} = [8,\\; 9,\\; 10].$$\n",
    "\n",
    "总体来说，这段代码展示了如何从数学上定义的行向量转化为 NumPy 数组，并通过索引与切片操作实现对向量元素的访问与提取，同时强调了 Python 中零索引与数学中常见的一索引之间的差别。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c9bb237-e550-470a-96e5-e38c5cee92a7",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fe4b0588-7422-4a90-88cd-a20136621b27",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:55:40.754966Z",
     "start_time": "2025-05-15T14:55:40.144582Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入NumPy库，用于数组操作和数值计算\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91e9446f-6db2-4162-80cc-fc41f3f6439c",
   "metadata": {},
   "source": [
    "## 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c0e20636-513e-491f-b699-3d83c17ba25b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:55:47.113274Z",
     "start_time": "2025-05-15T14:55:47.110933Z"
    }
   },
   "outputs": [],
   "source": [
    "# 定义一个包含 10 个元素的 Python 列表\n",
    "a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "914a3b7f-455f-4eaa-912f-4d7070711ba7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:55:51.691871Z",
     "start_time": "2025-05-15T14:55:51.688110Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "adf52e20-5214-4ff3-a10b-b8406a02c45a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:56:00.364660Z",
     "start_time": "2025-05-15T14:56:00.362289Z"
    }
   },
   "outputs": [],
   "source": [
    "# 将 Python 列表转换为 NumPy 数组\n",
    "a_ = np.array(a_list)\n",
    "# a_ = np.arange(1,11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "73b638fd-d53f-4142-b836-20bf8b78be4f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:56:04.038627Z",
     "start_time": "2025-05-15T14:56:04.035218Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "df6755a7-2a07-4998-aac1-4c6f31c13091",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:56:26.592842Z",
     "start_time": "2025-05-15T14:56:26.590445Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取数组的维度（即数组的轴数，1D数组的维度为1）\n",
    "dim = a_.ndim\n",
    "dim  # 输出数组的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bb3b7b41-614b-46d7-8cf0-a57150b65a5c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:56:36.274388Z",
     "start_time": "2025-05-15T14:56:36.271471Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(10,)"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取数组的形状（即数组每个轴上的元素个数）\n",
    "shape = a_.shape\n",
    "shape  # 输出数组的形状"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65755ab3-9a79-40c7-bbfd-9f252a330018",
   "metadata": {},
   "source": [
    "## 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "403c74c4-a3f9-42bf-a7f7-5b4caac9c82d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:56:39.177328Z",
     "start_time": "2025-05-15T14:56:39.174272Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问数组的第一个元素（索引从 0 开始）\n",
    "a_[0]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "72f235bd-056f-4f9a-bad9-8695dbebba60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问数组的第二个元素\n",
    "a_[1]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d87f8132-41ba-4420-ac39-2f603c7c163e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:56:48.636744Z",
     "start_time": "2025-05-15T14:56:48.633838Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "10"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问数组的最后一个元素，索引 -1 表示倒数第一个\n",
    "a_[-1]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e4764bfb-13dc-4bae-b3b1-5918fe81f3fa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-15T14:56:52.805535Z",
     "start_time": "2025-05-15T14:56:52.802248Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "9"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问数组的倒数第二个元素\n",
    "a_[-2]  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddb2d919-0c25-48c9-9c71-1f13d3b677dc",
   "metadata": {},
   "source": [
    "## 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "44fea2bb-29b8-4aec-89e4-744292f2ecb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取数组的前两个元素（索引范围 [0,2)）\n",
    "a_[0:2]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6d7c41ca-c305-40e8-997d-c7f6254759a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用简写的切片方式，获取数组的前两个元素\n",
    "a_[:2]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2b82984d-04fe-4a3b-a293-bfc1a97da3bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取从索引 1 开始的所有元素（从第二个到最后一个）\n",
    "a_[1:]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fd249d94-59fa-4969-937e-611aea294e63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8,  9, 10])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取数组的最后三个元素\n",
    "a_[-3:]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c2f7bc67-463d-4772-a759-4872af780d64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8,  9, 10])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取索引 7 及之后的所有元素（即最后三个元素）\n",
    "a_[7:]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d530bc5e-a5b5-41cf-92e4-461541be8fc2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3d3085ce-ec28-46dc-bbae-b965586ec691",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
